package io.github.talelin.latticy.common;

import io.github.talelin.latticy.entity.User;
import io.github.talelin.latticy.model.UserDO;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class UserUtil {

    private static ThreadLocal<User> local = new ThreadLocal<>();

    public static void setLocalUser(User user) {
        UserUtil.local.set(user);
    }

    public static User getLocalUser() {
        return UserUtil.local.get();
    }

    public static <T> T getLocalUser(Class<T> clazz) {
        return (T) local.get();
    }

    public static void clearLocalUser() {
        UserUtil.local.remove();
    }

    public static Long getUserId() {
        User user = UserUtil.getLocalUser();
        return user.getId();
    }
}
